from sqlmodel import SQLModel,Field, create_engine,Session,select
class Note(SQLModel, table=True):
    __tablename__: str = 'notes'

    id: int = Field(primary_key=True, default=None)
    content: str

sqlite_filepath = 'notes.db'
sqlite_url = f'sqlite:///{sqlite_filepath}'

engine = create_engine(sqlite_url)
SQLModel.metadata.create_all(engine)

with Session(engine) as session:
    """
    增加，添加操作
    note1 = Note(content='This is a note')
    note2 = Note(content='This is another note')
    session.add(note1)
    session.add(note2)
    session.commit()
    session.refresh(note1)
    print(note1)
    """
    """
    # 查询操作
    stmt = select(Note).where(Note.id % 2 == 0)
    result = session.exec(stmt)
    note = session.get(Note, 1)
    for note in result:
        print(note)
    """
    """
    # 更改操作
    stmt = select(Note).where(Note.id % 2 == 0)
    result = session.exec(stmt)
    note = session.get(Note, 1)
    for note in result:
        note.content = 'Updated'
        session.add(note)    
    session.commit()
    """
    """
    # 删除操作
    stmt = select(Note).where(Note.id % 2 == 0)
    result = session.exec(stmt)
    note = session.get(Note, 1)
    for note in result:
        session.deleted(note)
    session.commit()
    """


